<template>
	<div class="yellow">
		<div class="header flex a-center jlr">
			<div class="back" @click="back"><i class="iconfont icon-arrow-left-copy"></i></div>
			<h2>修改密码</h2>
			<div class="back">&nbsp;</div>
		</div>
		
		<div class="abox">
			<div class="login">
				<div class="loginbox">
					<div class="loginItem flex a-center">
						<input type="tel" placeholder="请输入手机号" maxlength="11" v-model="obj.phone" @input="cinput">
						<div class="icon-close" @click="openclose" v-if="obj.phone"></div>
					</div>
					
					<div class="logingroup flex a-center jlr">
						<div class="loginItem flex a-center" style="width: 60%; margin-top: 0;">
							<input type="tel" placeholder="请输入验证码" maxlength="6" v-model="obj.smsCode">
						</div>
						<button :disabled="isdisabled" @click="sendSmsCode">{{verifyname}}</button>
					</div>
					
					<div class="loginItem flex a-center">
						<input type="password" placeholder="请输入密码" v-model="obj.password">
					</div>
					
					<div class="loginItem flex a-center">
						<input type="password" placeholder="请输入新密码" v-model="obj.newpassword">
					</div>
					
					
					<div class="blockbtn" @click="login" style="margin-top: 0.4rem;">确认修改</div>
					
				</div>
			</div>
			
		</div>
		
	</div>
	
</template>
<script>
export default {
	data(){
		return {
			type:true,
			isdisabled:true,
			verifyname:'获取验证码',
			obj:{
				smsCode:'',
				phone:'',
				newpassword:'',
				password:''
			}
		}
	},
	methods:{
		enter(v){
			this.$router.push(v)
		},
		back(){
			this.$router.go(-1)
		},
		cinput(){
			var that = this
			var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
			if(myreg.test(that.obj.phone)){
				that.isdisabled = false
			}else{
				that.isdisabled = true
			}
		},
		sendSmsCode(){
			let num = 60
			var timer = null
			var that = this
			if(!that.isdisabled){
				that.isdisabled = true
				that.$ajax.sendSmsCode({phone:that.obj.phone}).then(res=>{				
					--num
					that.verifyname = num+'s'
					timer = setInterval(()=>{
						--num
						that.verifyname = num+'s'
						if(num<=0){
							clearInterval(timer)
							that.isdisabled = false
							that.verifyname = '获取验证码'
						}
					},1000)
				})
			}			
		},
		openclose(){
			this.obj.phone = ''
		},
    openpwd(){
			this.type = !this.type
		},
		login(){
			var that = this
			var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
			if(!myreg.test(that.obj.phone)){
				that.$toast.fail('手机号格式不正确')
			}else if(!that.obj.smsCode){
				that.$toast.fail('请输入验证码')
			}else if(!that.obj.password){
				that.$toast.fail('请输入密码')
			}else if (!that.obj.newpassword){
        that.$toast.fail('请输入新密码')
      } else{
				that.$ajax.updatePwd({...that.obj}).then(res=>{
					if(res.code==0){``
					  localStorage.clear();
						// localStorage.setItem('token',res.data.token)
            this.$router.replace('/')
					}					
				})
			}
			
		}
	}
}
</script>
